package cn.botter.auth.application.controller;

import cn.botter.auth.application.convert.AuthPermissionConvert;
import cn.botter.auth.application.dto.AuthPermissionDTO;
import cn.botter.auth.application.dto.AuthRoleDTO;
import cn.botter.auth.application.exception.RegistrationException;
import cn.botter.auth.domain.entity.AuthPermissionBO;
import cn.botter.auth.domain.service.AuthPermissionDomainService;
import cn.botter.auth.entity.Result;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @BelongsProject: bo-club
 * @BelongsPackage: cn.botter.application.controller
 * @Author: 爱写Bug的Botter
 * @CreateTime: 2025-03-17:14:11
 * @Description: 描述
 * @Version: 1.0
 */
@RestController
@RequestMapping("/permission/")
@Slf4j
public class AuthPermissionController {

    @Resource
    private AuthPermissionDomainService authPermissionDomainService;

    /**
     * 添加用户权限信息
     */
    @RequestMapping("add")
    public Result<AuthRoleDTO> add(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            // 昵称校验：非空
            Preconditions.checkArgument(
                    !StringUtils.isEmpty(authPermissionDTO.getName()),
                    "用户权限名称不能为空"
            );
            // 用户权限key不能为空和空串
            Preconditions.checkArgument(
                    authPermissionDTO.getPermissionKey() != null && !authPermissionDTO.getPermissionKey().trim().isEmpty(),
                    "用户权限Key不能为空"
            );
            if (log.isInfoEnabled()){
                log.info("AuthRoleController.add.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
//            AuthRoleBO authRoleBO = AuthRoleConvert.INSTANCE.convertDtoToBO(authRoleDTO);
            AuthPermissionBO authPermissionBO = AuthPermissionConvert.INSTANCE.convertDtoToBo(authPermissionDTO);
            Boolean result = authPermissionDomainService.add(authPermissionBO);
            if (result!=null && result){
                return Result.OK("添加用户权限成功!", result);
            }
            else {
                throw new RegistrationException("服务器异常");
            }
        } catch (Exception e) {
            log.info("AuthRoleController.add.error:{}", e.getMessage());
            return Result.FAILED("添加用户权限失败!",e.getMessage());
        }
    }


    /**
     * 修改用户权限信息
     */
    @RequestMapping("update")
    public Result<AuthRoleDTO> update(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            Preconditions.checkNotNull(authPermissionDTO.getId() , "用户权限id不能为空");
            if (log.isInfoEnabled()){
                log.info("AuthRoleController.update.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            AuthPermissionBO authPermissionBO = AuthPermissionConvert.INSTANCE.convertDtoToBo(authPermissionDTO);
            Boolean result = authPermissionDomainService.update(authPermissionBO);
            if (result!=null && result){
                return Result.OK("修改用户权限成功!", result);
            }
            else {
                throw new RegistrationException("服务器异常");
            }
        } catch (Exception e) {
            log.info("AuthRoleController.update.error:{}", e.getMessage());
            return Result.FAILED("修改用户权限失败!",e.getMessage());
        }
    }

    /**
     * 删除用户权限信息
     */
    @RequestMapping("delete")
    public Result<AuthRoleDTO> delete(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            // 昵称校验：非空
            Preconditions.checkNotNull(authPermissionDTO.getId(),"用户权限id不能为空");
            if (log.isInfoEnabled()){
                log.info("AuthRoleController.delete.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            AuthPermissionBO authPermissionBO = AuthPermissionConvert.INSTANCE.convertDtoToBo(authPermissionDTO);
            Boolean result = authPermissionDomainService.delete(authPermissionBO);
            if (result!=null && result){
                return Result.OK("删除用户权限成功!", result);
            }
            else {
                throw new RegistrationException("服务器异常");
            }
        } catch (Exception e) {
            log.info("AuthRoleController.delete.error:{}", e.getMessage());
            return Result.FAILED("删除用户权限失败!",e.getMessage());
        }
    }

    /**
     * 获取用户权限
     */
    @RequestMapping("getPermission")
    public Result<List<String>> getPermission(@RequestParam String userName) {
        try {
            // 昵称校验：非空
            System.out.println(userName);
            Preconditions.checkArgument(!StringUtils.isEmpty(userName),"用户名不能为空！");
            if (log.isInfoEnabled()){
                log.info("AuthRoleController.getPermission.userName:{}", JSON.toJSONString(userName));
            }
            List<String> result = authPermissionDomainService.gerPermission(userName);
            return Result.OK(result);
        } catch (Exception e) {
            log.info("AuthRoleController.getPermission.error:{}", e.getMessage());
            return Result.FAILED("获取用户权限失败!",e.getMessage());
        }
    }

    @RequestMapping("test")
    public String test(String userName) {
        return userName;
    }
}
